package com.jiazhong.basic.pack0315.work;

import com.jiazhong.basic.pack0315.work.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class Work {

    // 有⼀任意⻓度任意内容集合，求出集合中所有偶数之和
    private static void work01() {
        List<Integer> list = new ArrayList<>();
        int len = (int) (Math.random() * 10 + 3);
        for (int i = 0; i < len; i++) {
            int num = (int) (Math.random() * 100 + 1);
            list.add(num);
        }
        System.out.println(list);
        AtomicInteger sum = new AtomicInteger();
        list.forEach(e -> {
            if (e % 2 == 0) {
                // 自➕ sum.getAndAdd(e);
                int i = sum.get();
                i = i + e;
                sum.set(i);
            }
        });
        System.out.println("和是：" + sum.get());
    }

    // 有⼀任意⻓度任意内容集合，去除集合中重复的元素
    private static void work02() {
        List<Integer> list = new ArrayList<>();
        int len = (int) (Math.random() * 20 + 3);
        for (int i = 0; i < len; i++) {
            int num = (int) (Math.random() * 10 + 1);
            list.add(num);
        }
        System.out.println(list);
        for (int i = list.size() - 1; i > 0; i--) {
            for (int j = i - 1; j > -1; j--) {
                int my = list.get(i);
                int it = list.get(j);
                if (my == it) {
                    list.remove(i);
                    break;
                }
            }
        }
        System.out.println(list);
    }

    // 有⼀任意⻓度任意内容集合，找出那个元素出现的次数最多
    private static void work03() {
        List<Integer> list = new ArrayList<>();
        int len = (int) (Math.random() * 19 + 3);
        for (int i = 0; i < len; i++) {
            int num = (int) (Math.random() * 10 + 1);
            list.add(num);
        }
        System.out.println(list);
        List<Integer> counts = new ArrayList<>();
        for (int i = 0; i < list.size() - 1; i++) {
            int count = 1;
            for (int j = i + 1; j < list.size(); j++) {
                int my = list.get(i);
                int it = list.get(j);
                if (my == it) {
                    count++;
                }
            }
            counts.add(count);
        }
        System.out.println("每个元素出现的次数是：" + counts);
        int maxIndex = 0;
        for (int i = 1; i < counts.size(); i++) {
            if (counts.get(maxIndex) < counts.get(i)) {
                maxIndex = i;
            }
        }
        System.out.println("最大的元素下标是：" + maxIndex);
        System.out.println("出现次数最多的元素是：" + list.get(maxIndex));
    }

    // 有⼀个存放学⽣信息的集合（学⽣属性：编号、姓名和年龄），求出⽐张三年龄⼤的学⽣（选作）
    private static void work04() {
        List<Student> list = new ArrayList<>();
        Student s1 = new Student(1, "张三", (int) (Math.random() * 22 + 18));
        Student s2 = new Student(2, "李四", (int) (Math.random() * 22 + 18));
        Student s3 = new Student(3, "王五", (int) (Math.random() * 22 + 18));
        Student s4 = new Student(4, "马六", (int) (Math.random() * 22 + 18));
        Student s5 = new Student(5, "赵七", (int) (Math.random() * 22 + 18));
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        list.forEach(System.out::println);
        Student s = null;
        for (Student student : list) {
            if (student.getName().equals("张三")) {
                s = student;
                break;
            }
        }
        System.out.println("------------------------------");
        for (Student it : list) {
            if (it.compareTo(s) > 0) {
                System.out.println(it);
            }
        }
        // 排序后张三位置之后的元素
    }

    // 有⼀个存放边⻓的集合，计算集合中的边⻓能够组成多少种不同的三⻆形
    private static void work05() {
        List<String> bian = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        int len = (int) (Math.random() * 5 + 3);
        for (int i = 0; i < len; i++) {
            int num = (int) (Math.random() * 12 + 1);
            list.add(num);
        }
        System.out.println(list);
        for (int i = 0; i < list.size() - 2; i++) {
            for (int j = i + 1; j < list.size() - 1; j++) {
                for (int k = j + 1; k < list.size(); k++) {
                    int a = list.get(i);
                    int b = list.get(j);
                    int c = list.get(k);
                    if (a + b > c && a + c > b && b + c > a) {
                        List<Integer> s = new ArrayList<>();
                        s.add(a);
                        s.add(b);
                        s.add(c);
                        Collections.sort(s);
                        bian.add(s.toString());
                    }
                }
            }
        }
        bian.forEach(System.out::println);
        // 去重
        System.out.println("----------------------------------------------");
        for (int i = bian.size() - 1; i > 0; i--) {
            String s = bian.get(i);
            for (int j = i - 1; j > -1; j--) {
                String l = bian.get(j);
                if (s.equals(l)) {
                    bian.remove(s);
                    i--;
                }
            }
        }
        bian.forEach(System.out::println);
    }

    // 给定⼀个⼤⼩为 n 的集合 nums ，返回其中的多数元素。多数元素是指在集合中出现次数 ⼤于 ⌊ n/2 ⌋ 的元素。
    private static void work06() {
        List<Integer> list = new ArrayList<>();
        int len = (int) (Math.random() * 13 + 3);
        for (int i = 0; i < len; i++) {
            int num = (int) (Math.random() * 3 + 1);
            list.add(num);
        }
        System.out.println(list);
        List<Integer> counts = new ArrayList<>();

        for (int i = 0; i < list.size() - 1; i++) {
            int count = 0;
            for (int j = i + 1; j < list.size(); j++) {
                if (list.get(i) == list.get(j)) {
                    count++;
                }
            }
            counts.add(count);
        }
        System.out.println(counts);
        for (int i = 0; i < counts.size(); i++) {
            if (counts.get(i) > list.size() / 2) {
                System.out.println("多数元素是：" + list.get(i) + ",出现的次数是：" + counts.get(i));
            }
        }
    }

    // 给你⼀个⻓度为 n 的整数集合 nums 和 ⼀个⽬标值 target 。请你从 nums 中选出三个整数，使它们的和与 target 最接近。
    private static void work07() {
        List<Integer> nums = new ArrayList<>();
        int len = (int) (Math.random() * 15 + 3);
        for (int i = 0; i < len; i++) {
            int num = (int) (Math.random() * 10 + 1);
            nums.add(num);
        }
        int target = (int) (Math.random() * 20 + 5);
        System.out.println(nums);
        System.out.println("目标值：" + target);
        int cha = 100;
        String sgs = "";
        for (int i = 0; i < nums.size() - 2; i++) {
            for (int j = i + 1; j < nums.size() - 1; j++) {
                for (int k = j + 1; k < nums.size(); k++) {
                    int a = nums.get(i);
                    int b = nums.get(j);
                    int c = nums.get(k);
                    int abs = Math.abs(a + b + c - target);
                    if (abs < cha) {
                        cha = abs;
                        sgs = a + "," + b + "," + c;
                    }
                }
            }
        }
        System.out.println("三个数：" + sgs);
    }

    public static void main(String[] args) {
        work07();
    }
}
